{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3D Visualization and Maps\n",
    "\n",
    "Ipyvolume and pythreejs demos are below.  These require separately installing these widgets.\n",
    "[Ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) will be forthcoming, see [#6903](https://github.com/twosigma/beakerx/issues/6903). We hope these APIs will be included upstream in the original widget repositories.\n",
    "\n",
    "## ipyvolume\n",
    "\n",
    "There is an alpha Groovy API for [ipyvolume](https://github.com/maartenbreddels/ipyvolume). For this to work, the widget JS needs to be installed:\n",
    "```\n",
    "conda install -c conda-forge ipyvolume\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%classpath add mvn com.github.twosigma ipyvolume master-SNAPSHOT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipyvolume.PyLab;\n",
    "\n",
    "int size = 32;\n",
    "int radius = 12;\n",
    "float[][][] data = new float[size][size][size];\n",
    "for (int x = 0; x<size; x++){\n",
    "    for (int y = 0; y<size; y++){\n",
    "        for (int z = 0; z<size; z++){\n",
    "            if (Math.pow(x - size/2, 2) + Math.pow(y - size/2, 2) + Math.pow(z - size/2, 2) <= Math.pow(radius, 2)){\n",
    "                data[x][y][z] = 1.0f;\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "figure = PyLab.volShow(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## pythreejs\n",
    "\n",
    "There is an alpha Groovy API for [pythreejs](https://github.com/jupyter-widgets/pythreejs). For this to work, the widget JS needs to be installed:\n",
    "```\n",
    "conda install -c conda-forge pythreejs\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%classpath add mvn com.github.twosigma pythreejs master-SNAPSHOT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pythreejs.*\n",
    "\n",
    "f = \"function f(origu,origv) {\\n\" +\n",
    "          \"    // scale u and v to the ranges I want: [0, 2*pi]\\n\" +\n",
    "          \"    var u = 2*Math.PI*origu;\\n\" +\n",
    "          \"    var v = 2*Math.PI*origv;\\n\" +\n",
    "          \"    \\n\" +\n",
    "          \"    var x = Math.sin(u);\\n\" +\n",
    "          \"    var y = Math.cos(v);\\n\" +\n",
    "          \"    var z = Math.cos(u+v);\\n\" +\n",
    "          \"    \\n\" +\n",
    "          \"    return new THREE.Vector3(x,y,z)\\n\" +\n",
    "          \"}\"\n",
    "surf_g = new ParametricGeometry(func: f, slices: 16, stacks: 16);\n",
    "\n",
    "surf = new Mesh(geometry: surf_g, material: new MeshLambertMaterial(color: 'green', side: 'FrontSide'))\n",
    "surf2 = new Mesh(geometry: surf_g, material: new MeshLambertMaterial(color: 'yellow', side: 'BackSide'))\n",
    "c = new PerspectiveCamera(position: [5, 5, 3], up: [0, 0, 1],\n",
    "                      children: [new DirectionalLight(color: 'white',\n",
    "                                                 position: [3, 5, 1],\n",
    "                                                 intensity: 0.6d)])\n",
    "scene = new Scene(children: [surf, surf2, c, new AmbientLight(intensity: 0.5d)])\n",
    "renderer = new Renderer(camera: c, scene: scene, controls: [new OrbitControls(controlling: c)], _width: 400, _height: 400)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "beakerx_kernel_parameters": {},
  "kernelspec": {
   "display_name": "Groovy",
   "language": "groovy",
   "name": "groovy"
  },
  "language_info": {
   "codemirror_mode": "groovy",
   "file_extension": ".groovy",
   "mimetype": "",
   "name": "Groovy",
   "nbconverter_exporter": "",
   "version": "2.4.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": false,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": false,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
